﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormQuanlythongtinsach : Form
    {
        private CXulythongtinsach xulyTTS;
        private CXulyNhaxuatban xulyNXB;
        private Cxulytheloai xulyTL;
        private Cxulytacgia xulyTG;
        string ma = "";

        public FormQuanlythongtinsach()
        {
            InitializeComponent();
        }

        private void FormQuanlythongtinsach_Load(object sender, EventArgs e)
        {
            xulyTTS = new CXulythongtinsach();
            xulyNXB = new CXulyNhaxuatban();
            xulyTL = new Cxulytheloai();
            xulyTG = new Cxulytacgia();

            cbbNXB.DataSource = xulyNXB.GetdsNhaXuatBan();
            cbbNXB.DisplayMember = "NhaXuatBanName";
            cbbNXB.ValueMember = "NhaXuatBanName";

            cbbTheLoai.DataSource = xulyTL.getdsTheLoai();
            cbbTheLoai.DisplayMember = "TheLoaiName";
            cbbTheLoai.ValueMember = "TheLoaiName";

            cbbTG.DataSource = xulyTG.getdsTacgia();
            cbbTG.DisplayMember = "TacGiaName";
            cbbTG.ValueMember = "TacGiaName";

            cbbNXB.SelectedIndex = -1;
            cbbTheLoai.SelectedIndex = -1;
            cbbTG.SelectedIndex = -1;

            hienthidsSach(xulyTTS.getdsSach());
        }

        #region cac ham chuc nang
        private void hienthidsSach(List<Sach> ds_sach)
        {
            lvsach.Items.Clear();
            foreach (Sach sach in ds_sach)
            {
                addSachlv(sach);
            }
        }

        private void addSachlv(Sach sach)
        {
            if (sach.SachStatus == "live")
            {
                ListViewItem lvi = lvsach.Items.Add(sach.SachNo);
                lvi.SubItems.Add(sach.SachName);
                lvi.SubItems.Add(sach.NhaXuatBan.NhaXuatBanName);
                lvi.SubItems.Add(sach.SachNamXuatBan);
                lvi.SubItems.Add(hienthiSachTacGia(sach));
                lvi.SubItems.Add(hienthiSachTheLoai(sach));
                lvi.SubItems.Add(sach.SachPrice.ToString());
            }
        }

        private string hienthiSachTacGia(Sach sach)
        {
            string s = "";
            foreach (Sach_TacGia stg in sach.Sach_TacGias)
            {
                s += stg.TacGia.TacGiaName + " ";
            }
            return s;
        }
        private string hienthiSachTheLoai(Sach sach)
        {
            string s = "";
            foreach (Sach_TheLoai stl in sach.Sach_TheLoais)
            {
                s += stl.TheLoai.TheLoaiName + " ";
            }
            return s;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvsach.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                xulyTTS.XoaSach(ma);
            }
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormEditSach fedit = new FormEditSach(0,"");
            fedit.ShowDialog();
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvsach.SelectedItems)
            {
                ma = lvi.SubItems[0].Text;
            }
            FormEditSach fedit = new FormEditSach(1, ma);
            fedit.ShowDialog();
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lvsach.Items.Clear();
            List<Sach> dsSach = new List<Sach>();
            List<Sach> dstam = new List<Sach>();

            foreach (Sach sach in xulyTTS.getdsSach())
                dsSach.Add(sach);

            foreach (Sach sach in xulyTTS.getdsSach())
                dstam.Add(sach);

            if (txtMaSach.Text == "" && txtNamXB.Text == "" && cbbTG.Text == "" && txtTenSach.Text == "" && cbbNXB.Text == "" && cbbTheLoai.Text == "")
            {
                hienthidsSach(xulyTTS.getdsSach());
                return;
            }

            if (txtMaSach.Text != "")
            {
                addSachlv(xulyTTS.TimSachtheoma(txtMaSach.Text));
                return;
            }

            if (cbbNXB.Text != "")
            {
                foreach (Sach s in dsSach)
                    if (cbbNXB.Text != s.NhaXuatBan.NhaXuatBanName)
                        dstam.Remove(s);
                dsSach = dstam;
            }

            if (cbbTheLoai.Text != "")
            {
                foreach (Sach s in dsSach)
                {
                    bool flag = true;
                    foreach (Sach_TheLoai stl in s.Sach_TheLoais)
                        if (stl.TheLoai.TheLoaiName == cbbTheLoai.Text)
                            flag = false;
                    if (flag)
                        dstam.Remove(s);
                }
                dsSach = dstam;
            }

            if (cbbTG.Text != "")
            {
                foreach (Sach s in dsSach)
                {
                    bool flag = true;
                    foreach (Sach_TacGia stg in s.Sach_TacGias)
                        if (stg.TacGia.TacGiaName == cbbTG.Text)
                            flag = false;
                    if (flag)
                        dstam.Remove(s);
                }
                dsSach = dstam;
            }

            if (txtNamXB.Text != "")
            {
                foreach (Sach s in dsSach)
                    if (txtNamXB.Text != s.SachNamXuatBan)
                        dstam.Remove(s);
                dsSach = dstam;
            }

            if (txtTenSach.Text != "")
            {
                string ten = txtTenSach.Text;
                ten = ten.ToUpper();
                foreach (Sach s in dsSach)
                    if (!s.SachName.ToUpper().Contains(ten))
                        dstam.Remove(s);
                dsSach = dstam;
            }
            hienthidsSach(dsSach);
        }

        #endregion

    }
}
